package com.example.zsr.downloadcontinue;

import android.content.Context;
import android.content.Intent;
import android.net.TrafficStats;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.zsr.downloadcontinue.ToolUtils.ContantsUtils;
import com.example.zsr.downloadcontinue.ToolUtils.FileStatus;
import com.example.zsr.downloadcontinue.ToolUtils.SharePreferencesUtils;
import com.example.zsr.downloadcontinue.db.DBThreadListener;
import com.example.zsr.downloadcontinue.db.DBThreadListenerImp;
import com.example.zsr.downloadcontinue.download.DownloadService;
import com.example.zsr.downloadcontinue.entity.FileInfo;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengshaorui on 2017/2/23.
 */

public class DownlistAdapter extends BaseAdapter{
    private static final String TAG = "DownlistAdapter";

    private boolean isPause = false;

    private List<FileInfo> mFileInfoList;
    private List<FileStatus> mStatusList = new ArrayList<FileStatus>();
    private Context mContext;
    private ProgressBar mProgressBar;

    private boolean isupdate = false;
    private DBThreadListener mDbThreadListener;





    public DownlistAdapter(List<FileInfo> mFileInfoList, Context mContext) {
        this.mFileInfoList = mFileInfoList;
        this.mContext = mContext;
        mDbThreadListener  = new DBThreadListenerImp(mContext);

    }


    @Override
    public int getCount() {
        return mFileInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return mFileInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder{
        ImageView file_pause;
        TextView fileName, fileFinished,fileprogress,filestatus;
        ProgressBar fileSeekBar;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final FileInfo fileInfo = mFileInfoList.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.myprogress,null);
            viewHolder = new ViewHolder();
            viewHolder.file_pause = (ImageView) convertView.findViewById(R.id.file_pause);
            viewHolder.fileName = (TextView) convertView.findViewById(R.id.file_name);
            viewHolder.fileFinished = (TextView) convertView.findViewById(R.id.file_finished);
            viewHolder.fileSeekBar = (ProgressBar)convertView.findViewById(R.id.file_bar);
            viewHolder.fileprogress = (TextView) convertView.findViewById(R.id.file_progress);
            viewHolder.filestatus = (TextView)convertView.findViewById(R.id.file_status);
            viewHolder.fileSeekBar.setMax(100);
            viewHolder.fileName.setText(fileInfo.getFileName());
            //初始化，先全部改成false;
            FileStatus fileStatus = new FileStatus(position,fileInfo.getApkurl(),true);
            mStatusList.add(fileStatus);
            convertView.setTag(viewHolder);
        }else{
           viewHolder = (ViewHolder) convertView.getTag();
        }


        viewHolder.fileSeekBar.setTag(fileInfo.getApkurl()+ContantsUtils.TAG_PROGRESSBAR);
        viewHolder.fileprogress.setTag(fileInfo.getApkurl()+ContantsUtils.TAG_TEXTPROGRESS);
        viewHolder.fileFinished.setTag(fileInfo.getApkurl()+ContantsUtils.TAG_FILESIZE);
        viewHolder.filestatus.setTag(fileInfo.getApkurl()+ContantsUtils.TAG_FILESTATUS);
        viewHolder.file_pause.setTag(fileInfo.getApkurl()+ContantsUtils.TAG_FILEBTN);

        viewHolder.fileName.setText(fileInfo.getFileName());
        //设置进度条
        int progress = SharePreferencesUtils.getIntData(ContantsUtils.FILE_PROGRESS,
                fileInfo.getApkurl());
        if (progress > 0){
            viewHolder.fileSeekBar.setProgress(progress);
            viewHolder.fileprogress.setText(progress+"%");

            if (progress == 100) viewHolder.filestatus.setText("已完成");
            else  viewHolder.filestatus.setText("已暂停");
        }
        else {
            viewHolder.fileSeekBar.setProgress(0);
            viewHolder.fileprogress.setVisibility(View.INVISIBLE);
        }

        float filedownload = SharePreferencesUtils.getFloatData(ContantsUtils.FLOAT_FILEDOWNLOAD,fileInfo.getApkurl());
        float filesize = SharePreferencesUtils.getFloatData(ContantsUtils.FLOAT_FILESIZE,fileInfo.getApkurl());
        if (filedownload > 0 && filesize >0) {
            DecimalFormat decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
            String mCurProgress = decimalFormat.format(filedownload);//format 返回的是字符串
            String mProgressLength = decimalFormat.format(filesize);
            viewHolder.fileFinished.setText(mCurProgress+"MB"+" / "+mProgressLength+"MB");
        }else viewHolder.fileFinished.setVisibility(View.INVISIBLE);





        final ImageView mImageView = viewHolder.file_pause;
        final TextView  mTextView = viewHolder.filestatus;
        Log.d(TAG, "getView: "+position+" "+fileInfo.getApkurl());
        viewHolder.file_pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!mStatusList.get(position).getIspause()) {
                    mStatusList.get(position).setIspause(true);
                    mTextView.setText("正在下载");
                    mImageView.setImageResource(R.mipmap.pause);
                    Intent start = new Intent(mContext, DownloadService.class);
                    fileInfo.setBtnstatus(ContantsUtils.START);
                    start.putExtra("fileinfo", fileInfo);

                    mContext.startService(start);

                }else{
                    mStatusList.get(position).setIspause(false);
                    mImageView.setImageResource(R.mipmap.start);
                    mTextView.setText("已暂停");

                    Intent stop = new Intent(mContext, DownloadService.class);
                    fileInfo.setBtnstatus(ContantsUtils.PAUSE);
                    stop.putExtra("fileinfo", fileInfo);

                    mContext.startService(stop);
                }

                Log.d(TAG, "onClick: "+mStatusList.get(position)+" "+fileInfo);
            }
        });


        return convertView;
    }



    private void getNetSpeed(){
        long nowtotalbtye  = TrafficStats.getTotalRxBytes();
    }
}
